package com.leo.blog.login.service;




import com.leo.blog.dto.BlogerExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.login.SelfUserEntity;
import com.leo.blog.service.BlogerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 15:16
 * @since JDK 1.8
 */
@Component
public class SelfUserDetailsService implements UserDetailsService {

    @Autowired
    private BlogerService blogerService;

    /**
     * 查询用户信息
     * @Author Sans
     * @CreateTime 2019/9/13 17:23
     * @Param  username  用户名
     * @Return UserDetails SpringSecurity用户信息
     */
    @Override
    public SelfUserEntity loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        BlogerExecution bloger=blogerService.getUserByLoginName(username);
        if (bloger!=null && bloger.getBloger()!=null){
            Bloger b=bloger.getBloger();
            // 组装参数
            SelfUserEntity selfUserEntity = new SelfUserEntity();
            selfUserEntity.setUserId(b.getBlogerId()+"");
            selfUserEntity.setRealname(b.getBlogerName());
            selfUserEntity.setUsername(b.getUserName());
            selfUserEntity.setPassword(b.getPassword());
            return selfUserEntity;
        }
        return null;
    }
}
